package com.west.common.widget.banner;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.view.Gravity;

/**
 * 数字指示灯
 *
 * @author hanyulang
 * @date 2018/11/21
 */
public class NumberPagerIndicator extends AppCompatTextView implements PagerIndicator {

    private ViewPager mViewPager;
    private ViewPager.OnPageChangeListener mListener;

    private int mCurrentPage = 0;
    private int mFollowPage;

    public NumberPagerIndicator(Context context) {
        super(context);
        init();
    }

    public NumberPagerIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public NumberPagerIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        initBorder();
    }

    private void initBorder() {
        setTextSize(11);
        setGravity(Gravity.CENTER);
        setSingleLine(true);
        setLines(1);
        setTextColor(Color.parseColor("#FFFFFF"));

        float radius = 48f;
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);
        gradientDrawable.setShape(GradientDrawable.RECTANGLE);
        gradientDrawable.setDither(true);
        gradientDrawable.setColor(Color.parseColor("#E1E1E1"));
        gradientDrawable.setCornerRadius(radius);
        setBackground(gradientDrawable);
    }

    @Override
    public void bindViewPager(ViewPager view) {
        if (mViewPager == view) {
            return;
        }
        if (view.getAdapter() == null) {
            throw new IllegalStateException("ViewPager does not set adapter");
        }
        mViewPager = view;
        mViewPager.addOnPageChangeListener(this);
        setTextValue();
    }


    @Override
    public void bindViewPager(ViewPager view, int initialPosition) {
        bindViewPager(view);
        setCurrentItem(initialPosition);
    }

    @Override
    public void setCurrentItem(int item) {
        if (mViewPager == null) {
            throw new IllegalStateException("indicator has not bind ViewPager");
        }
        mViewPager.setCurrentItem(item);
        mCurrentPage = item;
        setTextValue();
    }

    @Override
    public void setOnPageChangeListener(ViewPager.OnPageChangeListener listener) {
        mListener = listener;
    }

    @Override
    public void notifyDataSetChanged() {
        setTextValue();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        mCurrentPage = position;
        if (mListener != null) {
            mListener.onPageScrolled(position, positionOffset, positionOffsetPixels);
        }
    }

    @Override
    public void onPageSelected(int position) {
        mCurrentPage = position;
        mFollowPage = position;
        setTextValue();
        if (mListener != null) {
            mListener.onPageSelected(position);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (mListener != null) {
            mListener.onPageScrollStateChanged(state);
        }
    }

    private void setTextValue(){
        if(mViewPager == null){
            return;
        }
        int count = mViewPager.getAdapter().getCount();
        if(count == 0){
            return;
        }
        if(mCurrentPage >= count){
            setCurrentItem(count - 1);
        }
        setText(String.format("%s/%s", (mCurrentPage + 1), count));
    }

}
